package com.premedsidekick;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class CourseRequirements extends Activity {
	Resources res;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	CheckBox biology;
	CheckBox chemistry;
	CheckBox ochem;
	CheckBox physics;
	String filename;
	boolean b_biology;
	boolean b_chemistry;
	boolean b_ochem;
	boolean b_physics;
	/*
	byte[] buffer;
	
	FileOutputStream output;
	FileInputStream input;
	*/
		
	public OnClickListener BiologyCheck = new OnClickListener() {
		public void onClick(View view) {
			if (!biology.isChecked()) {
				editor.putBoolean("biology_check", false);
				biology.setChecked(false);
				//System.out.println(buffer[0]);
			}
			else {
				editor.putBoolean("biology_check", true);
				biology.setChecked(true);
			}
			editor.commit();
		}
	};
			/*
			if (!biology.isChecked()) {
				System.out.println("Hello");
				biology.setChecked(false);
				buffer[0] = 0;
				//System.out.println(buffer[0]);
			}
			else {
				biology.setChecked(true);
				buffer[0] = 127;
			}
			try {
				System.out.println("Writing: " + buffer[0]);
				output.write(buffer);
			} catch (Exception e) {
				
			}
		}
		*/

	public OnClickListener ChemistryCheck = new OnClickListener() {
		public void onClick(View view) {
			if (!chemistry.isChecked()) {
				editor.putBoolean("chemistry_check", false);
				chemistry.setChecked(false);
			}
			else {
				editor.putBoolean("chemistry_check", true);
				chemistry.setChecked(true);
			}
			editor.commit();
		}
	};
	public OnClickListener OchemCheck = new OnClickListener() {
		public void onClick(View view) {
			if (!ochem.isChecked()) {
				editor.putBoolean("ochem_check", false);
				ochem.setChecked(false);
			}
			else {
				editor.putBoolean("ochem_check", true);
				ochem.setChecked(true);
			}
			editor.commit();
		}
	};
	public OnClickListener PhysicsCheck = new OnClickListener() {
		public void onClick(View view) {
			if (!physics.isChecked()) {
				editor.putBoolean("physics_check", false);
				physics.setChecked(false);
			}
			else {
				editor.putBoolean("physics_check", true);
				physics.setChecked(true);
			}
			editor.commit();
		}
	};
	
	public void onCreate (Bundle bundle) {
		super.onCreate (bundle);
		setContentView (R.layout.course_requirements);
		//setContentView (R.layout.test);
		//buffer = new byte[1];
		filename = "courserequirements_file";
		
		preferences = getSharedPreferences (filename, 0);
		editor = preferences.edit();
		b_biology = preferences.getBoolean("biology_check", false);
		b_chemistry = preferences.getBoolean("chemistry_check", false);
		b_ochem = preferences.getBoolean("ochem_check", false);
		b_physics = preferences.getBoolean("physics_check", false);
		/*
		try {
			output = openFileOutput(filename, Context.MODE_PRIVATE);
		} catch (Exception e) {
			// Handle exception here later
		}
		
		try {
			buffer[0] = 127;
			output.write(buffer);
		} catch (Exception e) {
			// Handle exception here later
		}
		
		try {
			input = openFileInput(filename);
		} catch (Exception e) {
			// Handle exception here later
		}
		try { 
			input.read(buffer);
			System.out.println("Read from file: " + buffer[0]);
		} catch (Exception e) {
			// handle this later again
		}
		 
		// must handle logic for a checked box
		if (buffer[0] == 127) {
			System.out.println(buffer[0]);
			biology.setChecked(true);
		}
		*/
		biology = (CheckBox) findViewById (R.id.course_requirements_biology_checkbox);
		biology.setOnClickListener(BiologyCheck);
		if (b_biology) {
			biology.setChecked(true);
		}
		
		chemistry = (CheckBox) findViewById (R.id.course_requirements_general_chemistry_checkbox); 
		chemistry.setOnClickListener(ChemistryCheck);
		if (b_chemistry) {
			chemistry.setChecked(true);
		}
		ochem = (CheckBox) findViewById (R.id.course_requirements_organic_chemistry_checkbox); 
		ochem.setOnClickListener(OchemCheck);
		if (b_ochem) {
			ochem.setChecked(true);
		}
		physics = (CheckBox) findViewById (R.id.course_requirements_general_physics_checkbox); 
		physics.setOnClickListener(PhysicsCheck);
		if (b_physics) {
			physics.setChecked(true);
		}
	}
	public void onPause (Bundle bundle) {
		System.out.println("Paused");
	}
	public void onDestroy(Bundle bundle) {
		System.out.println("Destroyed");
	}
	public void onKill(Bundle bundle) {
		System.out.println("Killed");
	}
	public void onStop(Bundle bundle) {
		System.out.println("Stopped");
	}
}
